# -*- coding: ISO-8859-1 -*-
require 'test/unit'
require 'model'

class TestPlaylistBuilder < Test::Unit::TestCase
	def setup
		@utopia = City.new("Utopia")
		@wc = Structure.new("Woodcutter", "Wood", [], Coord.new(5,0,0))
		@sm = Structure.new("Sawmill", "Boards", ["Wood"], Coord.new(0,-5,0))
		@utopia.build(@wc)
		@utopia.build(@sm)
	end
	
	def teardown
		@utopia = nil
		@wc = nil
		@sm = nil
	end
	
	def test_structures
		assert_equal("Utopia", @utopia.name)
		
		assert_equal({"Wood" => 0}, @wc.cache)
		assert_equal("Wood", @wc.produces)
		assert_equal([], @wc.demand)
		
		@wc.work
		assert_equal({"Wood" => 0}, @wc.cache)
		puts "Sleeping #{@wc.speed} seconds..."
		sleep(@wc.speed) # Allow wc time to work
		@wc.work
		assert_equal({"Wood" => 1}, @wc.cache)
		
		assert_equal({"Boards" => 0}, @sm.cache)
		assert_equal("Boards", @sm.produces)
		assert_equal(["Wood"], @sm.demand)
		
		@sm.work 
		assert_equal({"Boards" => 0}, @sm.cache) # sawmill has no Wood, so can't make Boards
		
		@wc.pickup("Wood")
		assert_equal({"Wood" => 0}, @wc.cache)
		
		@sm.deliver("Wood")
		assert_equal({"Wood" => 1, "Boards" => 0}, @sm.cache)
		
		@sm.work # not enough time has passed to allow the sawmill to work- so no change
		assert_equal({"Wood" => 1, "Boards" => 0}, @sm.cache)
		
		puts "Sleeping #{@sm.speed} seconds..."
		sleep(@sm.speed)
		
		@sm.work 
		assert_equal({"Wood" => 0, "Boards" => 1}, @sm.cache)
		
		sleep(@wc.speed - @sm.speed) # in case the wc takes longer to work than the sawmill
		@wc.work
		assert_equal({"Wood" => 1}, @wc.cache) 
	end
end
